package com.music.core;


import com.alibaba.fastjson.JSONObject;
import com.music.response.SongResponse;
import com.music.utils.MusicUtil;
import com.music.core.api.HttpEngine;
import com.music.core.api.MusicApi;
import com.music.core.api.MusicApiFactory;
import com.music.comstan.enums.MusicProviderEnum;
import com.music.core.model.Album;
import com.music.core.model.Song;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.util.List;

public class ShawnMain {

    public static void main(String[] args) throws IOException {
//        testKugouMusic();
        testNeteaseMusic();
//        testQQMusic();
//        testBaiduMusic();
//        testMiguMusic();
//        testYitingMusic();
//        testXiamiMusic();
//        testNeteaseAlbum();
//        testQQAlbum();
    }

    private static void testQQMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.QQ);
        List<? extends Song> result = api.searchMusicSync("孙燕姿", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }

    @SuppressWarnings("SpellCheckingInspection")
    private static void testNeteaseMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Netease);
        List<? extends Song> result = api.searchMusicSync("陈粒", 0, true);
        Song song = result.get(0);
        String string = JSONObject.toJSONString(song);
        System.out.println(string);
        SongResponse songResponse = MusicUtil.getSongResponse(song);
        System.out.println(songResponse.toString());
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }

    private static void testDownload(String url) throws IOException {
        Request.Builder builder = new Request.Builder();
        builder.url(url);
        builder.get();
        Response response = HttpEngine.requestSync(builder.build(), false);
        System.out.println("download music response code: " + response.code());
        response.close();
    }

    @SuppressWarnings("SpellCheckingInspection")
    private static void testBaiduMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Baidu);
        List<? extends Song> result = api.searchMusicSync("岳云鹏 送情郎", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }

    @SuppressWarnings("SpellCheckingInspection")
    private static void testMiguMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Migu);
        List<? extends Song> result = api.searchMusicSync("孙燕姿 第一天", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }


    @SuppressWarnings("SpellCheckingInspection")
    private static void testKugouMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Kugou);
        List<? extends Song> result = api.searchMusicSync("盛世繁华", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }

//    @SuppressWarnings("SpellCheckingInspection")
//    private static void testKuwoMusic() throws IOException {
//        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Kuwo);
//        List<? extends Song> result = api.searchMusicSync("Beyond", 0, true);
//        String url = result.get(0).getMusicLink().getUrl();
//        System.out.println(url);
//        testDownload(url);
//    }

    @SuppressWarnings("SpellCheckingInspection")
    private static void testXiamiMusic() throws IOException {    //hcy0224
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Xiami);
        List<? extends Song> result = api.searchMusicSync("五环之歌", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }

    @SuppressWarnings("SpellCheckingInspection")
    private static void testYitingMusic() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Yiting);
        List<? extends Song> result = api.searchMusicSync("Beyond", 0, true);
        String url = result.get(0).getMusicLink().getUrl();
        System.out.println(url);
        testDownload(url);
    }


    @SuppressWarnings("SpellCheckingInspection")
    private static void testNeteaseAlbum() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.Netease);
        Album album = api.getAlbumInfoByIdSync("37017747", true);
        System.out.println(album);
    }

    private static void testQQAlbum() throws IOException {
        final MusicApi api = MusicApiFactory.create(MusicProviderEnum.QQ);
        // Album album = api.getAlbumInfoByIdSync("001wmoL43SVFjj", true);
        Album album = api.getAlbumInfoByIdSync("000yiVfk1EpZIs", true);
        System.out.println(album);
    }
}
